﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace MyDLL
{
    /// <summary>
    /// 进程控制
    /// </summary>
    public class ProcessControl
    {
        /// <summary>
        /// 通过进程名称获取进程（一个）。
        /// </summary>
        /// <param name="processName">进程名称</param>
        /// <returns>返回一个整形，如果未找到符合条件的进程，则返回0。</returns>
        public static int GetProcessByName(string processName)
        {
            Process[] arrProcess = Process.GetProcessesByName(processName);
            foreach (Process p in arrProcess)
            {
                return p.Id;
            }
            return 0;
        }

        /// <summary>
        /// 通过进程名称获取进程（一个或多个）。
        /// </summary>
        /// <param name="processName">进程名称</param>
        /// <returns>返回整形数组</returns>
        public static int[] GetProcessesByName(string processName)
        {
            Process[] arrProcess = Process.GetProcessesByName(processName);
            int[] resArr = new int[arrProcess.Length];
            for (int i = 0; i < resArr.Length; i++)
            {
                resArr[i] = arrProcess[i].Id;
            }
            return resArr;
        }

        /// <summary>
        /// 按进程名称关闭进程。
        /// </summary>
        /// <param name="processName">进程名称</param>
        public void KillProcessesByName(string processName)
        {
            Process[] arrProcess = Process.GetProcessesByName(processName);
            foreach (Process p in arrProcess)
            {
                p.Kill();
            }
        }
    }
}
